using System;
using System.Collections;
using System.Collections.Generic;
using LiteNetLib;
using Protocols;
using UnityEngine;
using UnityEngine.AI;

public class SceneLogic : MonoBehaviour
{
    [SerializeField] private OtherPlayer OtherPlayerPrefab;

    private Dictionary<long, OtherPlayer> Players = new Dictionary<long, OtherPlayer>();

    public void AddPlayer(long id, MoveData? data = null)
    {
        if (Players.ContainsKey(id))
        {
            NetDebug.Log($"[AddPlayer] {id} already exists!");
            return;
        }

        var newPlayer = Instantiate(OtherPlayerPrefab);
        newPlayer.gameObject.SetActive(true);

        if (data != null)
        {
            var val = data.Value;
            newPlayer.transform.SetPositionAndRotation(new Vector3(val.pos_x, val.pos_y, val.pos_z),
                new Quaternion(val.rotate_x, val.rotate_y, val.rotate_z, val.rotate_w));
        }

        Players.Add(id, newPlayer.GetComponent<OtherPlayer>());
    }

    public void RemovePlayer(long id)
    {
        if (!Players.TryGetValue(id, out var player))
        {
            NetDebug.Log($"[RemovePlayer] {id} not exists!");
            return;
        }

        if (player != null)
        {
            Destroy(player.gameObject);
        }

        Players.Remove(id);
    }

    public void MovePlayer(long id, MoveData data)
    {
        if (!Players.TryGetValue(id, out var player))
        {
            NetDebug.Log($"[MovePlayer] {id} not exists!");
            return;
        }

        player.Move(data);
    }
}
